#!/bin/bash
# 化学反应验证与优化系统 - 演示脚本
# Chemical Reaction Validation and Optimization System - Demo Script

echo "╔════════════════════════════════════════════════════════════════════════════╗"
echo "║          化学反应验证与优化系统 - 完整演示                                 ║"
echo "╚════════════════════════════════════════════════════════════════════════════╝"
echo ""

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 函数：打印分隔线
print_separator() {
    echo "════════════════════════════════════════════════════════════════════════════"
}

# 函数：打印步骤
print_step() {
    echo -e "${BLUE}【$1】${NC}"
    print_separator
}

# 函数：打印成功
print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

# 函数：打印警告
print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

# 函数：打印错误
print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# 函数：等待用户按键
wait_for_key() {
    echo ""
    echo -e "${YELLOW}按任意键继续...${NC}"
    read -n 1 -s
    echo ""
}

# 主菜单
show_menu() {
    clear
    echo "╔════════════════════════════════════════════════════════════════════════════╗"
    echo "║          化学反应验证与优化系统 - 演示菜单                                 ║"
    echo "╚════════════════════════════════════════════════════════════════════════════╝"
    echo ""
    echo "请选择演示内容:"
    echo ""
    echo "  1. 📚 查看项目文档"
    echo "  2. 🧪 运行验证测试程序（5个案例）"
    echo "  3. 🔄 运行集成工作流程示例"
    echo "  4. 📊 查看规则库内容"
    echo "  5. 📁 查看项目文件结构"
    echo "  6. 📈 查看测试报告"
    echo "  7. 🎯 完整演示（全部内容）"
    echo "  0. 退出"
    echo ""
    echo -n "请输入选项 [0-7]: "
}

# 1. 查看项目文档
show_documentation() {
    clear
    print_step "项目文档"
    echo ""
    echo "可用文档列表:"
    echo ""
    ls -lh *.md 2>/dev/null | awk '{print "  📄 " $9 " (" $5 ")"}'
    echo ""
    echo "核心文档:"
    echo "  ⭐ README_验证系统.md - 系统索引和快速开始"
    echo "  ⭐ 反应验证系统使用指南.md - 完整使用指南"
    echo "  ⭐ 化学反应研制流程总结.md - 流程图和架构"
    echo "  ⭐ 项目完成报告.md - 项目完成报告"
    echo "  ⭐ 最终项目总结.md - 最终总结"
    echo ""
    echo -n "是否查看某个文档? (输入文件名或按Enter跳过): "
    read doc_name
    if [ -n "$doc_name" ]; then
        if [ -f "$doc_name" ]; then
            less "$doc_name"
        else
            print_error "文件不存在: $doc_name"
        fi
    fi
    wait_for_key
}

# 2. 运行验证测试程序
run_validation_tests() {
    clear
    print_step "运行验证测试程序"
    echo ""
    print_success "启动测试程序..."
    echo ""
    python3 test_reaction_validation.py
    echo ""
    print_success "测试完成！"
    wait_for_key
}

# 3. 运行集成工作流程
run_integrated_workflow() {
    clear
    print_step "运行集成工作流程示例"
    echo ""
    print_success "启动集成示例..."
    echo ""
    python3 integrated_workflow_example.py
    echo ""
    print_success "集成示例完成！"
    echo ""
    echo "生成的报告:"
    if [ -f "data/integrated_example1/comprehensive_report.json" ]; then
        print_success "data/integrated_example1/comprehensive_report.json"
        echo ""
        echo -n "是否查看报告内容? (y/n): "
        read view_report
        if [ "$view_report" = "y" ] || [ "$view_report" = "Y" ]; then
            python3 -m json.tool data/integrated_example1/comprehensive_report.json | less
        fi
    fi
    wait_for_key
}

# 4. 查看规则库
show_rules() {
    clear
    print_step "查看规则库内容"
    echo ""
    echo "规则库文件: reaction_rules.yaml"
    echo "规则库大小: $(wc -l < reaction_rules.yaml) 行"
    echo ""
    echo "规则分类:"
    echo "  1. 基本化学规则 (basic_rules)"
    echo "  2. 热力学规则 (thermodynamic_rules)"
    echo "  3. 反应条件规则 (reaction_conditions)"
    echo "  4. 能耗规则 (energy_rules)"
    echo "  5. 经济性规则 (economic_rules)"
    echo "  6. 安全规则 (safety_rules)"
    echo "  7. 环保规则 (environmental_rules)"
    echo "  8. 质量控制规则 (quality_control)"
    echo "  9. 优化建议规则 (optimization_suggestions)"
    echo "  10. 评分标准 (scoring_criteria)"
    echo ""
    echo -n "是否查看完整规则库? (y/n): "
    read view_rules
    if [ "$view_rules" = "y" ] || [ "$view_rules" = "Y" ]; then
        less reaction_rules.yaml
    fi
    wait_for_key
}

# 5. 查看项目文件结构
show_file_structure() {
    clear
    print_step "项目文件结构"
    echo ""
    echo "核心模块 (Python):"
    ls -lh *.py 2>/dev/null | awk '{print "  🐍 " $9 " (" $5 ")"}'
    echo ""
    echo "配置文件 (YAML/JSON):"
    ls -lh *.yaml *.json 2>/dev/null | awk '{print "  ⚙️  " $9 " (" $5 ")"}'
    echo ""
    echo "文档文件 (Markdown):"
    ls -lh *.md 2>/dev/null | awk '{print "  📄 " $9 " (" $5 ")"}'
    echo ""
    echo "数据目录:"
    if [ -d "data" ]; then
        ls -d data/*/ 2>/dev/null | awk '{print "  📁 " $1}'
    fi
    echo ""
    echo "文件统计:"
    echo "  Python文件: $(ls -1 *.py 2>/dev/null | wc -l) 个"
    echo "  配置文件: $(ls -1 *.yaml *.json 2>/dev/null | wc -l) 个"
    echo "  文档文件: $(ls -1 *.md 2>/dev/null | wc -l) 个"
    echo "  总代码行数: $(wc -l *.py *.yaml 2>/dev/null | tail -1 | awk '{print $1}') 行"
    wait_for_key
}

# 6. 查看测试报告
show_test_reports() {
    clear
    print_step "查看测试报告"
    echo ""
    echo "可用报告:"
    find data -name "*.txt" -o -name "*.json" 2>/dev/null | while read file; do
        echo "  📊 $file"
    done
    echo ""
    echo -n "输入报告路径查看内容 (或按Enter跳过): "
    read report_path
    if [ -n "$report_path" ] && [ -f "$report_path" ]; then
        if [[ "$report_path" == *.json ]]; then
            python3 -m json.tool "$report_path" | less
        else
            less "$report_path"
        fi
    fi
    wait_for_key
}

# 7. 完整演示
full_demo() {
    clear
    print_step "完整演示开始"
    echo ""
    print_success "这将依次演示所有功能..."
    wait_for_key
    
    # 1. 文档
    show_documentation
    
    # 2. 文件结构
    show_file_structure
    
    # 3. 规则库
    show_rules
    
    # 4. 验证测试
    run_validation_tests
    
    # 5. 集成工作流程
    run_integrated_workflow
    
    # 6. 测试报告
    show_test_reports
    
    clear
    print_step "完整演示结束"
    echo ""
    print_success "所有功能演示完成！"
    echo ""
    echo "项目亮点:"
    echo "  ✅ 完整的反应验证体系"
    echo "  ✅ 能耗和经济性分析"
    echo "  ✅ 多维度综合评估"
    echo "  ✅ 智能优化建议"
    echo "  ✅ 完整的文档和测试"
    echo ""
    wait_for_key
}

# 主循环
main() {
    while true; do
        show_menu
        read choice
        case $choice in
            1) show_documentation ;;
            2) run_validation_tests ;;
            3) run_integrated_workflow ;;
            4) show_rules ;;
            5) show_file_structure ;;
            6) show_test_reports ;;
            7) full_demo ;;
            0) 
                clear
                echo ""
                echo "感谢使用化学反应验证与优化系统！"
                echo ""
                exit 0
                ;;
            *)
                print_error "无效选项，请重新选择"
                sleep 2
                ;;
        esac
    done
}

# 检查是否在正确的目录
if [ ! -f "reaction_validator.py" ]; then
    print_error "请在 react 目录下运行此脚本"
    echo "使用方法: cd react && bash demo.sh"
    exit 1
fi

# 启动主程序
main

